/*
   @Copyright:LibreOJ
   @Author:   tjyemail
   @Problem:  https://loj.ac/problem/123
   @Language: C++
   @Datetime: 20-07-30 11:07
   */
// Optimize Prim with Mini-Heap, time O(ElogV)

#include <bits/stdc++.h>
using namespace std;

struct Edge {
	int v, c;
	Edge(int v=0, int c=0) : v(v), c(c){}
	bool operator<(const Edge &e) const { return c>e.c; } // mini-heap
};
bool visited[200005];
vector<Edge> edges[200005];

void prim(int s) {
	long long cost = 0, c = 0;
	priority_queue<Edge> vcosts; // mini-heap - vertex, cost
	for(vcosts.push({s, 0}); !vcosts.empty(); ){
		s=vcosts.top().v, c=vcosts.top().c;
		vcosts.pop();
		if (visited[s]) continue;
		// add edges from s's adjacency vertex
		for(const Edge &e:edges[s]) {
			if (visited[e.v]) continue;
			vcosts.push(e);
		}
		visited[s] = true;
		cost += c;
	}
	// todo: check it is MST
	printf("%lld\n", cost);
}

int main()
{
	int v, e, a, b, c;
	scanf("%d %d", &v, &e); // v vertexs, e edges
	for(; e--;) {
		scanf("%d %d %d", &a, &b, &c); // a->b, cost
		edges[a].push_back(Edge(b, c));
		edges[b].push_back(Edge(a, c));
	}
	prim(1); // start vertex from [1, v]
	return 0;
}
